********************************************************************
*This file builds industry shift-componenent of shift-share measure*
********************************************************************

*Open robot data*
use "$clean_data_automation/robot_data_ifr19.dta", replace

*reshape before merging*
gen stock_=stock_b19
keep stock_ industry country year
reshape wide stock_ , i(industry_ifr country) j(year)

***Merge data from KLEMS***
merge 1:1 country industry_ifr19 using "$clean_data_automation/klems_data_ifr19.dta", keep(3) nogenerate

***Create measures of exposure***
preserve
keep if country=="United States" 
tab country

*merge the output data from BEA (Note: EUKLEMS does not include data for output by US industry past 2004)*
merge 1:1 industry_ifr19 using "$contributed_data/bea_detailed_ifr19.dta", assert(3) nogenerate 

*Output in 1993=1997-average of following 3 years
gen ln_output_qty_1993 = ln(output_qty_1997)-(1997-1993)*(1/3)*((ln(output_qty_1998)-ln(output_qty_1997))+(ln(output_qty_1999)-ln(output_qty_1998))+(ln(output_qty_2000)-ln(output_qty_1999)))

forvalues j=2004(1)2014{
gen rstock_adj`j'=stock_`j'/EMP_1990-ln(output_qty_`j')*stock_1993/EMP_1990
gen rstock_lv`j'=stock_`j'/EMP_1990
}

gen rstock_adj1993=stock_1993/EMP_1990-ln_output_qty_1993*stock_1993/EMP_1990
gen rstock_lv1993=stock_1993/EMP_1990

gen rstock_adj2000=stock_2000/EMP_1990-ln(output_qty_2000)*stock_1993/EMP_1990
gen rstock_lv2000=stock_2000/EMP_1990

foreach var in lv adj{
gen apr_us_`var'93_=rstock_`var'1993
gen apr_us_`var'00_=rstock_`var'2000
gen apr_us_`var'04_=rstock_`var'2004
gen apr_us_`var'07_=rstock_`var'2007
gen apr_us_`var'14_=rstock_`var'2014
gen apr_us_`var'04_14_=rstock_`var'2014-rstock_`var'2004
gen apr_us_`var'07_14_=rstock_`var'2014-rstock_`var'2007
gen apr_us_`var'04_07_=rstock_`var'2007-rstock_`var'2004
gen apr_us_`var'93_00_=rstock_`var'2000-rstock_`var'1993
gen apr_us_`var'00_07_=rstock_`var'2007-rstock_`var'2000
gen apr_us_`var'93_07_=rstock_`var'2007-rstock_`var'1993
gen apr_us_`var'93_14_=rstock_`var'2014-rstock_`var'1993
}

rename EMP_1990 emp_us_
keep apr_us* industry emp_us_
save "$clean_data_automation/apr_measures_ifr19.dta", replace
restore

* Hours in the US to adjust european data. This ensures we compute robots per thousand (US) full time equivalent workers *
bysort industry: egen hours_us=total((H_EMP_1990/EMP_1990)*(country=="United States")) 

************************************************************
***				Measure 1:  EU7 countries	             ***
************************************************************
preserve
keep if  country=="Sweden" | country=="Finland" | country=="France" | country=="Denmark" | country=="Italy" | country=="Spain" | country == "United Kingdom"

forvalues j=1993(1)2007{
gen rstock_qo`j'=stock_`j'*hours_us/H_EMP_1990 - ln(GO_QI_`j')*stock_1993*hours_us/H_EMP_1990
gen rstock_lv`j'=stock_`j'*hours_us/H_EMP_1990
}

forvalues j=2008(1)2014{
gen rstock_qo`j'=stock_`j'*hours_us/H_EMP_1990 - (ln(GO_QI_2007)+ (`j'-2007)*(ln(GO_QI_2007)-ln(GO_QI_2000))/7)*stock_1993*hours_us/H_EMP_1990
gen rstock_lv`j'=stock_`j'*hours_us/H_EMP_1990
}

foreach var in qo lv {
gen apr_euro7_`var'93_=rstock_`var'1993
gen apr_euro7_`var'00_=rstock_`var'2000
gen apr_euro7_`var'07_=rstock_`var'2007
gen apr_euro7_`var'14_=rstock_`var'2014
gen apr_euro7_`var'93_00_=rstock_`var'2000-rstock_`var'1993
gen apr_euro7_`var'93_07_=rstock_`var'2007-rstock_`var'1993
gen apr_euro7_`var'93_14_=rstock_`var'2014-rstock_`var'1993
gen apr_euro7_`var'00_07_=rstock_`var'2007-rstock_`var'2000
gen apr_euro7_`var'00_14_=rstock_`var'2014-rstock_`var'2000
gen apr_euro7_`var'07_14_=rstock_`var'2014-rstock_`var'2007
}

*Take averages*
collapse (mean) apr_* rstock_*, by(industry_ifr)
merge 1:1 industry using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
save "$clean_data_automation/apr_measures_ifr19.dta", replace
restore

************************************************************
***	 Measure 2:  EU5 as in Acemoglu and Restrepo (2020)  ***
************************************************************
preserve
keep if  country=="Sweden" | country=="Finland" | country=="France" | country=="Denmark" | country=="Italy"
tab country

forvalues j=1993(1)2007{
gen rstock_qo`j'=stock_`j'*hours_us/H_EMP_1990 - ln(GO_QI_`j')*stock_1993*hours_us/H_EMP_1990
gen rstock_lv`j'=stock_`j'*hours_us/H_EMP_1990
}

forvalues j=2008(1)2014{
gen rstock_qo`j'=stock_`j'*hours_us/H_EMP_1990 - (ln(GO_QI_2007)+ (`j'-2007)*(ln(GO_QI_2007)-ln(GO_QI_2000))/7)*stock_1993*hours_us/H_EMP_1990 /*For 2007 onwards, we extrapolate production quantities from 2000-2007 onwards due to lack of data in EUKLEMS*/
gen rstock_lv`j'=stock_`j'*hours_us/H_EMP_1990
}

* Generate differences over different periods of time *
*foreach var in lv qo price {
foreach var in lv qo {
gen apr_euro5_`var'93_=rstock_`var'1993
gen apr_euro5_`var'00_=rstock_`var'2000
gen apr_euro5_`var'07_=rstock_`var'2007
gen apr_euro5_`var'14_=rstock_`var'2014
gen apr_euro5_`var'93_00_=rstock_`var'2000-rstock_`var'1993
gen apr_euro5_`var'93_07_=rstock_`var'2007-rstock_`var'1993
gen apr_euro5_`var'93_14_=rstock_`var'2014-rstock_`var'1993
gen apr_euro5_`var'00_07_=rstock_`var'2007-rstock_`var'2000
gen apr_euro5_`var'00_14_=rstock_`var'2014-rstock_`var'2000
gen apr_euro5_`var'07_14_=rstock_`var'2014-rstock_`var'2007

}


* Measures setting exposure to zero outside of manufacturing. Not used in final version of the paper. *
foreach period in 93_00_ 93_07_ 93_14_ 00_07_ 00_14_ 07_14_ {
gen apr_man_euro5_qo`period'=apr_euro5_qo`period'
replace apr_man_euro5_qo`period'=0 if industry_ifr=="services" | industry_ifr=="research" | industry_ifr=="construction" | industry_ifr=="utilities" | industry_ifr=="mining" | industry_ifr=="agriculture"
}

*Take averages*
collapse (mean) apr_* , by(industry_ifr)
merge 1:1 industry using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
save "$clean_data_automation/apr_measures_ifr19.dta", replace
restore


*************************************************************
***		   	Measure 3:  EU8 = EU7 + Germany		    	  ***
*************************************************************
preserve
drop if  country=="United States"|country=="Norway"
tab country

forvalues j=1993(1)2007{
gen rstock_qo`j'=stock_`j'*hours_us/H_EMP_1990 - ln(GO_QI_`j')*stock_1993*hours_us/H_EMP_1990
}

forvalues j=2008(1)2014{
gen rstock_qo`j'=stock_`j'*hours_us/H_EMP_1990 - (ln(GO_QI_2007)+ (`j'-2007)*(ln(GO_QI_2007)-ln(GO_QI_2000))/7)*stock_1993*hours_us/H_EMP_1990
}


gen apr_euro8_qo93_00_=rstock_qo2000-rstock_qo1993
gen apr_euro8_qo93_07_=rstock_qo2007-rstock_qo1993
gen apr_euro8_qo93_11_=rstock_qo2011-rstock_qo1993
gen apr_euro8_qo93_14_=rstock_qo2014-rstock_qo1993
gen apr_euro8_qo00_07_=rstock_qo2007-rstock_qo2000
gen apr_euro8_qo00_11_=rstock_qo2011-rstock_qo2000
gen apr_euro8_qo00_14_=rstock_qo2014-rstock_qo2000
gen apr_euro8_qo07_11_=rstock_qo2011-rstock_qo2007
gen apr_euro8_qo07_14_=rstock_qo2014-rstock_qo2007
gen apr_euro8_qo11_14_=rstock_qo2014-rstock_qo2011


*Take averages*
collapse (mean) apr_*, by(industry_ifr)
merge 1:1 industry using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
save "$clean_data_automation/apr_measures_ifr19.dta", replace
restore

****************************************************************************
***					Measure 4:  EU4 for robustness check				 ***
****************************************************************************
preserve
keep if  country=="Sweden" | country=="Finland"| country=="Denmark"| country=="Spain"

forvalues j=1993(1)2007{
gen rstock_qo`j'=stock_`j'*hours_us/H_EMP_1990 - ln(GO_QI_`j')*stock_1993*hours_us/H_EMP_1990
gen rstock_lv`j'=stock_`j'*hours_us/H_EMP_1990
}

forvalues j=2008(1)2014{
gen rstock_qo`j'=stock_`j'*hours_us/H_EMP_1990 - (ln(GO_QI_2007)+ (`j'-2007)*(ln(GO_QI_2007)-ln(GO_QI_2000))/7)*stock_1993*hours_us/H_EMP_1990
gen rstock_lv`j'=stock_`j'*hours_us/H_EMP_1990
}

gen apr_euro4_qo93_=rstock_qo1993
gen apr_euro4_qo00_=rstock_qo2000
gen apr_euro4_qo07_=rstock_qo2007
gen apr_euro4_qo14_=rstock_qo2014
gen apr_euro4_qo93_00_=rstock_qo2000-rstock_qo1993
gen apr_euro4_qo93_07_=rstock_qo2007-rstock_qo1993
gen apr_euro4_qo93_14_=rstock_qo2014-rstock_qo1993
gen apr_euro4_qo00_07_=rstock_qo2007-rstock_qo2000
gen apr_euro4_qo07_14_=rstock_qo2014-rstock_qo2007


*Take averages*
collapse (mean) apr_* rstock_*, by(industry_ifr)
merge 1:1 industry using "$clean_data_automation/apr_measures_ifr19.dta", assert(3) nogenerate
save "$clean_data_automation/apr_measures_ifr19.dta", replace
restore

use "$clean_data_automation/apr_measures_ifr19.dta", clear

order industry_ifr19 apr_us_adj* apr_us_lv* apr_euro7_qo* apr_euro7_lv* apr_euro5_qo* apr_euro5_lv* apr_euro8_qo* apr_euro4_qo*

save "$clean_data_automation/apr_measures_ifr19.dta", replace
